package com.hlddz.venue_reservation.controller;


import com.hlddz.venue_reservation.dto.ReserveDetailDto;
import com.hlddz.venue_reservation.dto.ReserveDto;
import com.hlddz.venue_reservation.entity.Reserve;
import com.hlddz.venue_reservation.entity.Visitor;
import com.hlddz.venue_reservation.service.ReserveService;
import com.hlddz.venue_reservation.service.ReserveVisitorService;
import com.hlddz.venue_reservation.service.VenueService;
import com.hlddz.venue_reservation.service.VisitorService;
import com.hlddz.venue_reservation.util.Result;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MoShang
 * @since 2024年05月31日
 */
@Slf4j
@RestController
@RequestMapping("/reserve")
public class ReserveController {

    @Resource
    private ReserveService reserveService;

    @Resource
    private VisitorService visitorService;

    @Resource
    private ReserveVisitorService reserveVisitorService;

    @Resource
    private VenueService venueService;

    /*预约参观
    输入数据: 用户名，场馆ID，预约日期，预约时间
    输出数据: 是否预约成功（成功，不成功）*/
    @PostMapping("/add")
    public Result addReserve(@RequestBody ReserveDto reserveDto){
        if(reserveDto == null) {
            ReserveDto nullReserveDto = new ReserveDto();
            nullReserveDto.setIsSuccess(false);
            return Result.error("500","错误");
        }
        Reserve reserve = new Reserve();
        reserve.setReserveDate(reserveDto.getReserveDate());
        //reserve.setReserveId(0);
        reserve.setUsername(reserveDto.getUsername());
        reserve.setVenueId(reserveDto.getVenueId());
        reserve.setReserveDate(reserveDto.getReserveDate());
        reserve.setReserveTime(reserveDto.getReserveTime());
        reserve.setStatus("已预约");
        reserve.setSubmitTime(LocalDate.now().toString());

        int reserveId = reserveService.doReserve(reserve);
        log.info(String.valueOf(reserve.getReserveId()));

        //add之后拿到对应的游客信息和订单信息
        List<Visitor> visitorInfo = visitorService.getVisitorInfo(reserve.getUsername());

        //if(reserve.getReserveId() != null){
            //int reserveId = reserve.getReserveId();
            //把每一个游客信息和订单信息结合写入Reserve-Visitor
            reserveVisitorService.addVisitorReserveInfo(visitorInfo,reserveId);
        //}


        //维护对应场馆的capacity和remain
        venueService.updateVenueRemain(reserve.getVenueId());
        reserveDto.setVenueRemain(venueService.getVenueRemainById(reserve.getVenueId()));
        //最后返回
        reserveDto.setIsSuccess(true);
        return Result.success(reserveDto);
    }
    @GetMapping("/getList")
    public Result getReserves(){
        List<Reserve> reserves = reserveService.getReserves();
        return Result.success(reserves);
    }

    @GetMapping("/getReserveListByUsername")
    public Result<List<ReserveDetailDto>> getReserveListByUsername (String username) {
        List<ReserveDetailDto> list=reserveService.getReserveListByName(username);
        return Result.success(list);
    }

    @PutMapping("/updateReserveStatus")
    public Result updateReserveStatus(@RequestParam("reserveId") int reserveId,@RequestParam("status") String status){
        reserveService.UpdateReserveStatus(reserveId,status);
        return Result.success();
    }


}

